package com.xblibo.xruth.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.xblibo.xruth.common.BaseEntity;

/**
 * <p>Title: User.java </p>
 * <p>Description:  </p>
 * <p>Copyright: 易宝 2011*版权所有 </p>
 * <p>Company: yeepay </p>
 * @author xblibo
 * @version 1.0 ,  2011  下午03:13:27
 */
@Entity
@Table(name="user")
public class User extends BaseEntity implements UserDetails {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Column(name="username",unique=true)
	private String username;
	private String password;
	
	
	@ManyToMany(targetEntity = Role.class, fetch = FetchType.LAZY)  
	@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))  
	private Set<Role> roles;  
	
	
	
	
	public Collection<GrantedAuthority> getAuthorities() {
		 List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(roles.size());  
	     Set<Function> functions = new HashSet<Function>();
		 for(Role role : roles) {  
			 for(Function  function : role.getFunctions()){
				 functions.add(function);
			 }
	     }  
		 for(Function function:functions){
			 grantedAuthorities.add( new SimpleGrantedAuthority(function.getShortName())); 
		 }
	     return grantedAuthorities;  
	}

	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public Set<Role> getRoles() {
		return roles;
	}


	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}


	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}
	
	public Map<String,Set<Function>> getModuleMenuFunction(){
		Map<String,Set<Function>> map = new HashMap<String,Set<Function>>();
		String key = null;
		Set<Function> value = null;
		for(Role role:getRoles()){
			for(Function function:role.getFunctions()){
				if(function.getIsMenu()){
					key = function.getModule().getShortName();
					value = map.get(key);
					if(value != null){
						map.get(key).add(function);
					}else{
						value = new HashSet<Function>();
						value.add(function);
						map.put(key, value);
					}
				}
			}
		}
		return map;
	}
	
	public Set<Module> getModules(){
		Set<Module> models = new HashSet<Module>();
		for(Role role:getRoles()){
			for(Function function:role.getFunctions()){
				models.add(function.getModule());
			}
		}
		return models;
	}
	
	
	 public boolean equals(Object obj)
	  {
	    if ((obj instanceof User)) {
	      return this.getId().equals(((User)obj).getId());
	    }
	    return false;
	  }

	  public int hashCode()
	  {
	    return this.getId().hashCode();
	  }
}
